/**
*      author: Y_Yao
*      created: 2024-11-09 22:54:21
**/

#include<bits/stdc++.h>

using namespace std;
#define int LL
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
int L,R,k;

bool check(int mid)
{
    if(mid * k <= R && mid * k >= L)
    {
        return true;
    }
    else return false;
}

void solve()
{
    cin >> L >> R >> k;
    int l = L - 1,r = R + 1;
    while(l < r)
    {
        int mid = l + (r - l + 1) / 2;
        if(check(mid)) l = mid;
        else r = mid - 1;
    }

    cout << l + 1 - L << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}